﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using System.Threading;
using System.Net.Mail;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="strSmtpServer">邮件服务器地址</param>
        /// <param name="strFrom">发件人地址</param> 
        /// <param name="strto">收件人</param>
        /// <param name="strSubject">标题</param>
        /// <param name="IsHtml">邮件格式是否为Html格式</param>
        /// <param name="strBody">内容</param>
        public static void SendSMTPEMail(String strSmtpServer, String strFrom,String strTo, String strSubject, Boolean IsHtml, String strBody)
        {
            try
            {
                System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);//服务器地址
                client.EnableSsl = true;//注意：一定要先设置 EnableSsl和UseDefaultCredentials，再实例化Credentials 
                client.UseDefaultCredentials = false;//注意：一定要先设置 EnableSsl和UseDefaultCredentials，再实例化Credentials 
                client.Credentials = new System.Net.NetworkCredential(strFrom, "ervstfcqjasrbghf"); //第1个参数:来源邮箱,第2个参数:腾讯给的smtp身份验证
                client.DeliveryMethod = SmtpDeliveryMethod.Network; //邮件发送方式
                client.Timeout = 100000; //超时设置
                System.Net.Mail.MailMessage message = new MailMessage(strFrom, strTo, strSubject, strBody);
                message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                message.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
                string attaches = @"E:\test.txt";//邮件地址
                Attachment aa = new Attachment(attaches);
                message.Attachments.Add(aa);
                message.IsBodyHtml = IsHtml;
                client.Send(message);
            }
            catch (Exception ex)
            {
                String err = ex.Message;
            }            
        }
       public static void SendEmailToUser()
        {
            String strSmtpServer = "smtp.qq.com";//服务器地址
            String strFrom = "327369636@QQ.com";//发件人地址           
            String strTo = "michaelchen@schmidtelectronics.com";//收件人地址
            String strSubject = "Email from EmailSendDemo";//标题
            Boolean IsHtml = true;
            String strBody = "Thank you for login EmailSendDemo website"; //邮件内容
            SendSMTPEMail(strSmtpServer, strFrom,  strTo, strSubject, IsHtml, strBody);//发送
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SendEmailToUser();
        }
    }
}
